<template>
  <div class="shopContainer">
    <div class="mui-card" v-for="(item,i) of shopinfo" :key="item.id">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
              <mt-switch v-model="$store.getters.getSwich[item.id]" @change="switchChange(item.id,$store.getters.getSwich[item.id])"></mt-switch>
              <img :src="item.thumb_path"/>
              <div class="shop-info">
                <h2>{{ item.title}}</h2>
                <p class="shop-price">
                  <span class="price">￥{{ item.sell_price}}</span>
                  <!-- [item.id]这里就可以直接用对象属性名来显示数量 -->
                  <shop-numbox :numCount="$store.getters.getCount[item.id]" :shopId="item.id"></shop-numbox>
                  <a href="#" @click.prevent="remove(item.id,i)">删除</a>
                </p>
              </div>
					</div>
				</div>
			</div>
      <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner jiesuan">
						<div class="left">
              <p>总计 (不含运费)</p>
              <p>已勾选商品 <span>{{ $store.getters.getShop.count}}</span> 件,总计 <span class="span">￥{{ $store.getters.getShop.price}}</span></p>
            </div>
            <mt-button type="danger" size="normal" @click="jiesuan">去结算</mt-button>
					</div>
				</div>
			</div>
  </div>
</template>

<script>
import shopNumbox from '../public/shopNumbox'
export default {
  name: "TabbarShop",
  data(){
    return{
      shopinfo:[]//购物车的数据
    }
  },
  mounted(){
    this.getshopinfo();
  },
  methods:{
    getshopinfo(){
      let arrId=[];//用来存取id
      //循环store仓库里面的car
      this.$store.state.car.forEach(item=>{
        arrId.push(item.id)
      })
      if(arrId.length===0){
        return;
      }
      this.$http.get('api/goods/getshopcarlist/'+arrId.join(',')).then(result=>{
        var data=result.body;
        if(data.status===0){
          this.shopinfo=data.message;
        }
      })
    },
    // 删除购物车商品
    remove(id,index){
       // 点击删除，把商品从 store 中根据 传递的 Id 删除，同时，把 当前组件中的 goodslist 中，对应要删除的那个商品，使用 index 来删除
      this.shopinfo.splice(index,1);
      this.$store.commit('removeid',id);
    },
    switchChange(id,val){
        // 每当点击开关，把最新的 快关状态，同步到 store 中
      this.$store.commit('swichChange',{id,selected:val});
    },
    // 结算跳转
    jiesuan(){
      this.$router.push('/shop/jiesuan')
    }
  },
  components:{
    shopNumbox
  }
};
</script>

<style lang="scss" scoped>
.shopContainer{
  background-color: #eee;
  overflow: hidden;
  .mui-card-content-inner{
    display: flex;
    justify-content: space-between;
    align-items: center;
    img{
      width: 1.2rem;
      height: 1.2rem;
      padding: 0 .1rem;
    }
    .shop-info{
      h2{
        font-size: .26rem;
        font-weight: 700;
        line-height: .32rem;
      }
      .shop-price{
        display: flex;
        justify-content: space-between;
        .price{
          color: red;
          font-weight: 700;
        }
      }
    }
  }
  .jiesuan{
    .left{
      p{
        span{
          color: red;
        }
        .span{
          font-weight: 700;
        }
      }
    }
  }
}
</style>